//
//  HYCustomSegementView.m
//  Mall
//
//  Created by 路贵斌 on 15/7/15.
//  Copyright (c) 2015年 _Zhizi_. All rights reserved.
//

#import "HYCustomSegementView.h"

@interface HYCustomSegementView ()

@property (nonatomic, weak) IBOutlet UIButton               * btn1;
@property (nonatomic, weak) IBOutlet UIButton               * btn2;
@property (nonatomic, weak) IBOutlet UIButton               * btn3;
//@property (nonatomic, weak) IBOutlet UIButton               * btn4;
@property (nonatomic, strong) UILabel                       * underLine;
//@property (nonatomic, weak) IBOutlet NSLayoutConstraint     *underLineWidth;


- (IBAction)buttonClicked:(UIButton *)button;

@end

@implementation HYCustomSegementView

+ (UINib *)nib
{
    return [UINib nibWithNibName:NSStringFromClass(self) bundle:nil];
}

- (void)awakeFromNib
{
    [self createUnderLine];
    self.backgroundColor=[UIColor whiteColor];
//    [self setButton:self.btn4 WithOriginalImage:@"choose.png"forState:UIControlStateNormal];
    [self setButton:self.btn3 WithOriginalImage:@"price_normal.png" forState:UIControlStateNormal];
    [self setButton:self.btn3 WithOriginalImage:@"price_advance.png" forState:UIControlStateSelected];
    //[self resetEdeWithButton:self.btn4];
    [self resetEdeWithButton:self.btn3];
    
}

- (void)createUnderLine
{
    self.underLine = [[UILabel alloc]initWithFrame:CGRectMake(14, self.height - 3 , DeviceWidth/3.0 - 28, 2)];
    self.underLine.backgroundColor = kcolorThemeNav;
    [self addSubview:self.underLine];
}
- (void)setButton:(UIButton *)button WithOriginalImage:(NSString * )imageName forState:(UIControlState )state
{
    [button setImage:[UIImage imageNamed:imageName]   forState:state];
}

- (void)resetEdeWithButton:(UIButton *)button
{
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 34 * 2  , 0, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 22 * 1);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}

- (IBAction)buttonClicked:(UIButton *)button
{
    _btn1.selected = NO;
    _btn2.selected = NO;
    _btn3.selected = NO;
   // _btn4.selected = NO;
    button.selected = YES;
    
    static BOOL advance = NO;
    
    if (button.tag == 3) {
        advance = !advance;
        [self setButton:self.btn3 WithOriginalImage:advance ?  @"price_advance.png" : @"price_inverted.png" forState:UIControlStateSelected];
    }else{
        advance = NO;
    }
    
    if (self.buttonBlock) {
        self.buttonBlock (button,advance);
    }
    
    [UIView animateKeyframesWithDuration:0.3 delay:0.0 options: UIViewKeyframeAnimationOptionCalculationModeLinear   animations:^{
        self.underLine.centerX =  button.centerX;
    } completion:^(BOOL finished) {
    
    }];
}


@end
